package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import java.awt.Component;

import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import model.MatHang;
import controller.MatHangController;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SuaSanPham extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SuaSanPham frame = new SuaSanPham(0);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SuaSanPham(final int ma) {
		setTitle("Sửa thông tin sản phẩm");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 405, 283);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblMMtHng = new JLabel("Mã mặt hàng");
		lblMMtHng.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblMNhCung = new JLabel("Mã nhà cung cấp");
		lblMNhCung.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblTnMtHng = new JLabel("Tên mặt hàng");
		lblTnMtHng.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblGi = new JLabel("Giá");
		lblGi.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblGiNhp = new JLabel("Giá nhập");
		lblGiNhp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lblSLng = new JLabel("Số lượng");
		lblSLng.setFont(new Font("Tahoma", Font.PLAIN, 13));
		MatHang m = MatHangController.getMatHang(ma);
		textField = new JTextField();
		textField.setColumns(10);
		textField.setText(ma + "");
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setText(Integer.toString(m.getIdNCC()));
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setText(m.getTenMH());
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setText(Double.toString(m.getGia()));
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setText(Double.toString(m.getGiaNhap()));
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setText(Integer.toString(m.getSoLuongMH()));
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(SuaSanPham.class.getResource("/icon/sua.gif")));
		
		JButton btnSa = new JButton("Đồng ý");
		btnSa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					model.MatHang m = new model.MatHang();
					m.setMaMH(Integer.parseInt(textField.getText()));
					m.setIdNCC(Integer.parseInt(textField_1.getText()));
					m.setTenMH(textField_2.getText());
					m.setGia(Double.parseDouble(textField_3.getText()));
					m.setGiaNhap(Double.parseDouble(textField_4.getText()));
					m.setSoLuongMH(Integer.parseInt(textField_5.getText()));
					dispose();
					MatHangController.updateMatHang(m);
					JOptionPane.showMessageDialog(null, "Sửa thông tin sản phẩm thành công");
				}catch(Exception e1){
					JOptionPane.showMessageDialog(null, "Đã xảy ra lỗi, vui lòng nhập lại");
				}
			}
		});
		btnSa.setForeground(new Color(139, 0, 0));
		btnSa.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JButton btnHy = new JButton("Hủy");
		btnHy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnHy.setForeground(new Color(139, 0, 0));
		btnHy.setFont(new Font("Tahoma", Font.BOLD, 13));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(lblMNhCung)
											.addGap(18)
											.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(lblTnMtHng)
											.addGap(18)
											.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(lblGi, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(lblMMtHng)
											.addGap(18)
											.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
									.addGap(27)
									.addComponent(label))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblGiNhp)
									.addGap(18)
									.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblSLng)
									.addGap(18)
									.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(105)
							.addComponent(btnSa, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnHy, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblMMtHng)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblMNhCung)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblTnMtHng)
								.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblGi)
								.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblGiNhp)
								.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblSLng)
								.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(26)
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSa, Alignment.TRAILING)
						.addComponent(btnHy, Alignment.TRAILING))
					.addContainerGap(13, Short.MAX_VALUE))
		);
		gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {lblMMtHng, lblMNhCung, lblTnMtHng, lblGi, lblGiNhp, lblSLng});
		contentPane.setLayout(gl_contentPane);
	}
}
